/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rules;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import types.ColoringInfo;
import types.ISudokuMatrix;
import utils.RuleHelper;

/**
 *
 * @author kostja
 */
public class QuadratOneFree implements Rule{

    int quadrat = 0;
    public String getName() {
        return QuadratOneFree.class.getSimpleName();
    }

    public String getPicName() {
       return "Anwendbar, wenn in einem Quadrat genau 8 Felder besetzt sind.";
    }

    public boolean isApplicable(int x, int y, ISudokuMatrix matrix) {
        System.out.println("Trying to apply rule "+ this.getName()+ " for "+x+","+y);
        try {
            this.setQuadrat(x, y);
            Collection col = RuleHelper.getDigitsSameQuadrat(x, y, matrix);
            Iterator iter = col.iterator();
            int counter = 0;
            while (iter.hasNext())
            {
             int num = (Integer)iter.next();
             if (num==0)
                 counter++;
             if(counter>1)
                 return false;
            }

        } catch (Exception ex) {
            Logger.getLogger(QuadratOneFree.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("\tRule applicable");
         return true;
    }

     /**
     *  Liefert das Kosten der Anwendung des Regels
     *  Kosten für Betrachtung des Quadrat selbst = 1
     *  Kosten für Betrachtung des direkt-benachbarten Quadrat = 2
     *  Kosten für Betrachtung des indirekt-benachbarten  (Nachbar des Nachbars)Quadrat = 3
     *  +++++++++++++
     *  + 1 + 2 + 3 +
     *  +++++++++++++
     *  + 4 + 5 + 6 +
     *  +++++++++++++
     *  + 7 + 8 + 9 +
     *  +++++++++++++
     * @param QuadratNumber
     * @return
     */
    public int getWeight(int QuadratNumber) {

       return 1;
    }
public int compareTo(Rule o) {
        if (this.getWeight(this.getQuadrat())<o.getWeight(o.getQuadrat()))
            return -1;
        else if (this.getWeight(this.getQuadrat())>o.getWeight(o.getQuadrat()))
            return 1;
        else
            return 0;
    }

    public void setQuadrat(int x, int y) throws Exception {

            this.quadrat = RuleHelper.calculateQuadratNumber(x, y);

    }

    public int getQuadrat() {
       return this.quadrat;
    }

    public ColoringInfo getCheckedUnits(int x, int y, ISudokuMatrix matrix) {
        try {
            Collection quadratL = new ArrayList();
            int qNr = RuleHelper.calculateQuadratNumber(x, y);
            quadratL.add(qNr);
            ColoringInfo colInfo = new ColoringInfo();
            colInfo.setBlocks(quadratL);
            return colInfo;
        } catch (Exception ex) {
            Logger.getLogger(QuadratOneFree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}
